package com.yj.ccs.abs.web.management.mapper;

import com.yj.ccs.abs.web.management.dto.ChangeStatusDto;
import com.yj.ccs.abs.web.management.dto.IdListDto;
import com.yj.ccs.abs.web.management.dto.UpdateUserDto;
import com.yj.ccs.abs.web.management.entity.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Copyright 2018 (C) Yunjian-VC
 * Created on : 2018/3/14
 * Author     : Unbelievable9
 **/
@Repository
@Mapper
public interface UserMapper {

    @Insert("INSERT INTO `user` (username, account, firm, apartment, area_code, cellphone, " +
            "role_type, active, `password`, password_salt, create_time, creator) " +
            "VALUES(#{item.username}, #{item.account}, #{item.firm}, #{item.apartment}, " +
            "#{item.area_code}, #{item.cellphone}, #{item.role_type}, #{item.active}, " +
            "#{item.password}, #{item.password_salt}, NOW(), #{item.creator})")
    void create(@Param("item") User item);

    @Insert("<script>" +
            "INSERT INTO `user` (username, account, firm, apartment, area_code, cellphone, " +
            "role_type, active, `password`, password_salt, create_time, creator) VALUES " +
            "<foreach collection='list' item = 'item' separator = ', '>" +
            "(#{item.username}, #{item.account}, #{item.firm}, #{item.apartment}, " +
            "#{item.area_code}, #{item.cellphone}, #{item.role_type}, #{item.active}, " +
            "#{item.password}, #{item.password_salt}, NOW(), #{item.creator})" +
            "</foreach>" +
            "</script>")
    void batchCreate(@Param("list") List<User> list);

    @Update("UPDATE `user` SET username = #{item.username}, account = #{item.account}, firm = #{item.firm}, apartment = #{item.apartment}, " +
            "area_code = #{item.area_code}, cellphone = #{item.cellphone}, role_type = #{item.role_type}, active = #{item.active} " +
            "WHERE id = #{item.id}")
    void update(@Param("item") UpdateUserDto item);

    @Update("UPDATE `user` SET password = #{password}, password_salt = #{password_salt} " +
            "WHERE id = #{id}")
    void changePassword(@Param("id") Long id,
                        @Param("password") String password,
                        @Param("password_salt") String password_salt);

    @Update("UPDATE `user` SET active = #{item.active} " +
            "WHERE id = #{item.id}")
    void changeStatus(@Param("item") ChangeStatusDto changeStatusDto);

    @Delete("<script>" +
            "DELETE FROM `user` WHERE id IN (" +
            "<foreach collection = 'item.id_list' item = 'id' separator = ', '>" +
            "#{id}" +
            "</foreach>" +
            ")" +
            "</script>")
    void batchDelete(@Param("item") IdListDto item);

    @Select("SELECT * FROM `user` WHERE id = #{id} LIMIT 1")
    User getById(@Param("id") Long id);

    @Select("SELECT * FROM `user` WHERE account = #{account} LIMIT 1")
    User getByAccount(@Param("account") String account);
}
